/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.popup.ctrl;

import com.arg.deploy.common.ProcessUbuntu;
import com.arg.deploy.webapp.common.MainPageController;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;

/**
 *
 * @author sorasaks
 */
public class PopupCtrl extends MainPageController {
    
    private static final Logger LOG = Logger.getLogger(PopupCtrl.class);
    public static final String PREFIX = "stream2file";
    public static final String SUFFIX = ".sh";
    public static final String EXECUTION_SCRIPT = "SCRIPT";
    public static final String EXECUTION_SCRIPT_FILENAME = "SCRIPT_FILENAME";
    public static final String EXECUTION_SCRIPT_FILEPATH = "SCRIPT_FILEPATH";
    private Label lbTotal;
    private Button btnDownload;
    private Button btnTag;
    private Grid gridScript;
    
    public PopupCtrl() {
        super("", Boolean.FALSE);
    }
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initialEventListener();
        initialRenderer();
    }
    
    private void initialRenderer() {
        List<String> script = (List<String>) arg.get(EXECUTION_SCRIPT);
        LOG.info(":::::::::::::::::::::: script.size() = " + script.size());
        lbTotal.setValue(String.valueOf(script.size()));
        gridScript.setModel(new ListModelList<String>(script));
    }
    
    private void initialEventListener() {
        btnDownload.addEventListener(Events.ON_CLICK, new DownloadScript());
        btnTag.addEventListener(Events.ON_CLICK, new TagScript());
    }

    private class TagScript implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            String filepath = (String) arg.get(EXECUTION_SCRIPT_FILEPATH);
            ProcessUbuntu process = new ProcessUbuntu(filepath);
            process.process();
            
            List<String> result = new ArrayList<String>();
            result.add(process.getOutputString());
            result.add(process.getErrorString());
            
            result.add("\n");
            result.add("Total Success : " + process.getOutputString().split("\n").length);
            
            gridScript.setModel(new ListModelList<String>(result));
        }
    }
    
    private class DownloadScript implements EventListener {
        
        @Override
        public void onEvent(Event t) throws Exception {
            String filename = (String) arg.get(EXECUTION_SCRIPT_FILENAME);
            
            LOG.info("::::::::::::::::::::::: filename = " + filename);
            File file = new File(filename);
            if (file.exists()) {
                Filedownload.save(file, null);
            }
        }
    }
}
